package dao.impl;


import dao.IJobSeekerDao;
import domain.JobSeeker;
import utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JobSeekerDaoImpl implements IJobSeekerDao {
    Connection conn =null;
    PreparedStatement pst =null;
    ResultSet rs =null;
    @Override
    public JobSeeker jobSeekerLogin(String jsaccount, String jspassword) {
        try {
            conn = JdbcUtils.getConnection();
            String sql="select * from jobseeker where jsaccount = ? and jspassword = ?";
            pst = conn.prepareStatement(sql);
            pst.setString(1,jsaccount);
            pst.setString(2,jspassword);
            rs = pst.executeQuery();
            if (rs.next()){
                JobSeeker jobSeeker = new JobSeeker();
                jobSeeker.setJsid(rs.getInt(1));
                jobSeeker.setJsaccount(rs.getString(2));
                jobSeeker.setJspassword(rs.getString(3));
                jobSeeker.setJsname(rs.getString(4));
                jobSeeker.setHeadIamge(rs.getString(5));
                jobSeeker.setBirthday(rs.getDate(6));
                jobSeeker.setEmail(rs.getString(7));
                jobSeeker.setTel(rs.getString(8));
                jobSeeker.setSex(rs.getString(9));
                jobSeeker.setLivecity(rs.getString(10));
                System.out.println(jobSeeker);
                return jobSeeker;
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
            JdbcUtils.close(rs,pst,conn);
        }
          return null;
    }

    @Override
    public int addJobSeeker(JobSeeker jobSeeker) {
        int i = 0;
        try {
             conn = JdbcUtils.getConnection();
            String sql="insert into jobseeker( jsaccount,jspassword,jsname,tel) values(?,?,?,?)";
             pst = conn.prepareStatement(sql);
            pst.setString(1,jobSeeker.getJsaccount());
            pst.setString(2,jobSeeker.getJspassword());
            pst.setString(3,jobSeeker.getJsname());
            pst.setString(4,jobSeeker.getTel());
            i = pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUtils.close(pst,conn);
        }

        return i;
    }
}
